﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Repository;
using Lead2012.Utility;

namespace Lead2012.Controllers
{
    public class CalendarItemController : BaseController
    {
        // Create an instance of repository
        CalendarItemManagementRepository repository = new CalendarItemManagementRepository();
        
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            CalendarItemViewModel model = new CalendarItemViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");
            model.PageSize = 10;
            model.PageIndex = 1;
            model.FromDate = LeadTools.StartOfDay(DateTime.Now.AddDays(-7));
            model.ToDate = LeadTools.EndOfDay(DateTime.Now);
            model.UserId = "0";
            repository.GetModel(ref model);

            return View(model);
        }

        [HttpPost]
        public ActionResult Filters(int ownerId, string startDate, string endDate, int pageIndex, int pageSize)
        {
            CalendarItemViewModel model = new CalendarItemViewModel();

            string[] arrStart = startDate.Split('/');
            string[] arrEnd = endDate.Split('/');

            DateTime start = DateTime.Parse(string.Format("{0}-{1}-{2}", arrStart[2], arrStart[1], arrStart[0]));
            start = LeadTools.StartOfDay(start);

            DateTime end = DateTime.Parse(string.Format("{0}-{1}-{2}", arrEnd[2], arrEnd[1], arrEnd[0]));
            end = LeadTools.EndOfDay(end);

            // Setup model
            model.PageIndex = pageIndex;
            model.PageSize = pageSize;
            model.UserId = ownerId.ToString();
            model.FromDate = start;
            model.ToDate = end;

            repository.GetModel(ref model);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            CalendarItemRepository cirepository = new CalendarItemRepository();
            CalendarItemViewModel model = new CalendarItemViewModel();

            // Delete action
            cirepository.Delete(id);

            // Setup model
            model.PageSize = 10;
            model.PageIndex = 1;
            model.FromDate = DateTime.Now.AddDays(-7);
            model.ToDate = DateTime.Now;
            repository.GetModel(ref model);

            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }
}
